package org.jxc.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.WarehouseDAO;
import org.jxc.domain.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class WarehouseService implements IService {

	private WarehouseDAO warehouseDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		warehouseDAO = (WarehouseDAO) DAOFactory.getDAO(sessionFactory,
				WarehouseDAO.class);
	}

	/**
	 * 分页
	 * 
	 * @param page
	 * @param superId
	 * @return
	 * @throws CRUDException
	 */
	public Page<Warehouse> listPagination(Page<Warehouse> page, String superId)
			throws CRUDException {
		if (Validate.notNull(superId))
			return warehouseDAO.pagination(page,"from Warehouse where deleteFlag='0' and id = ?", null, superId);
		else
			return warehouseDAO.pagination(page, "from Warehouse where deleteFlag='0'",
					null);
	}

	public List<Warehouse> findAll()
			throws CRUDException {
		return warehouseDAO.findAll();
	}

	public void save(Warehouse obj) throws CRUDException {
		warehouseDAO.save(obj);
	}

	public Warehouse findById(String id) throws CRUDException {
		return warehouseDAO.get(id);
	}

	public void update(Warehouse object) throws CRUDException {
		warehouseDAO.update(object);
	}

	public List<Warehouse> getList(Warehouse warehouse) throws CRUDException {
		return warehouseDAO.find(this.getQueryHql(warehouse), null);
	}

	private String getQueryHql(Warehouse warehouse) {
		StringBuffer sb = new StringBuffer(" from Warehouse where deleteFlag = '0' ");
		return sb.toString();
	}

	public void deleteWarehouse(String id) throws CRUDException {
		Warehouse warehouse = warehouseDAO.get(id);
		warehouse.setDeleteFlag("1");
		warehouseDAO.update(warehouse);
	}
}